package cn.add.ui;

import cn.add.R;
import cn.add.cache.CacheManage;
import cn.add.model.Signal;
import cn.add.util.DrawUtil;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;

public class SignalImage extends Component {

	private static final String TAG = "SignalImage";
	private static final boolean DBG = true;

	private Signal mSignal;

	private int mCurrentIndex = -1;

	private static final int STAT_FOUR = 4;
	private static final int STAT_THREE = 3;
	private static final int STAT_TWO = 2;
	private static final int STAT_ONE = 1;
	private static final int STAT_ZERO = 0;
	
	private final int[] images = new int[] { R.drawable.stat_sys_signal_0,
			R.drawable.stat_sys_signal_1, R.drawable.stat_sys_signal_2,
			R.drawable.stat_sys_signal_3, R.drawable.stat_sys_signal_4 };

	public SignalImage(Context context, Signal signal) {
		super(context);
		mSignal = signal;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		drawImage(canvas);
	}

	private void drawImage(Canvas mCanvas) {
		Bitmap mBitmap;

		if (mSignal != null) {
			int position = levelToPosition(mSignal.getASU());
			if (mCurrentIndex != position) {
				mCurrentIndex = position;
				mBitmap = CacheManage.getBitmap(images[position],
						images[position]);
			} else {
				mBitmap = CacheManage.getBitmap(images[mCurrentIndex],
						images[mCurrentIndex]);
				if (mBitmap == null) {
				}
			}
			DrawUtil.drawBitmap(mCanvas, mBitmap, 180, 380, null);
		}
	}

	private int levelToPosition(int level) {
		int position = -1;
		if (VersionInfo.getSDKVersion() <= 7) {
			if (level <= 0 || level == 99) position = STAT_ZERO;
            else if (level >= 16) position = STAT_FOUR;
            else if (level >= 8)  position = STAT_THREE;
            else if (level >= 4)  position = STAT_TWO;
            else position = STAT_ONE;
		}else {
			if(level<=2||level==99)position = STAT_ZERO;
			else if (level >= 12) position = STAT_FOUR;
            else if (level >= 8)  position = STAT_THREE;
            else if (level >= 5)  position = STAT_TWO;
            else position = STAT_ONE;
		}
		return position;
	}
}
